home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / MCI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.8 KB  |  254 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.5  $
  6. // Implements TMci and TMciHiddenWindow
  7. //----------------------------------------------------------------------------
  8. #include <owl/pch.h>
  9. #if !defined(OWL_MCI_H)
  10. # include <owl/mci.h>
  11. #endif
  12.  
  13. OWL_DIAGINFO;
  14.  
  15. //
  16. //
  17. //
  18. DEFINE_RESPONSE_TABLE1(TMciHiddenWindow, TWindow)
  19.   EV_MESSAGE(MM_MCINOTIFY, MciNotify),
  20. END_RESPONSE_TABLE;
  21.  
  22. //
  23. // A hidden window created for the sole purpose of catching MCI messages.
  24. //
  25. TMciHiddenWindow::TMciHiddenWindow(TMci& mci, TModule* module)
  26. :
  27.   Mci(mci),
  28.   TWindow(0, 0, module)
  29. {
  30.   Attr.Style = WS_POPUP;
  31. }
  32.  
  33. //
  34. // Notify the MCI class the MCI event has finished.
  35. //
  36. TResult
  37. TMciHiddenWindow::MciNotify(TParam1 wp, TParam2 lp)
  38. {
  39.   int32 retVal = Mci.MciNotify(wp, lp);
  40.   Mci.SetBusy(false);
  41.   return retVal;
  42. }
  43.  
  44. //
  45. // Creates a hidden window for catching messages.
  46. //
  47. TMci::TMci()
  48. :
  49.   WaitingForNotification(false),
  50.   DeviceId(0)
  51. {
  52.   // Create a hidden window for notifications
  53.   //
  54.   Window = new TMciHiddenWindow(*this);
  55.   Window->Create();
  56. }
  57.  
  58. //
  59. // If the MCI device is still open, closes it now.
  60. // Deletes the hidden window.
  61. //
  62. TMci::~TMci()
  63. {
  64.   if (DeviceId != 0)
  65.     Close();
  66.   delete Window;
  67. }
  68.  
  69. //
  70. // Return the callback.
  71. // If the window exist, the handle of the window is returned.
  72. //
  73. uint32
  74. TMci::GetCallback() const
  75. {
  76.   if (Window)
  77.     return uint32(HWND(*Window));
  78.   return 0;
  79. }
  80.  
  81. //
  82. // Send the MCI command to the device if not busy.
  83. //
  84. uint32
  85. TMci::SendCommand(uint msg, uint32 command, uint32 param)
  86. {
  87.   if (IsBusy())
  88.     return MCI_NOTIFY_ABORTED;
  89.  
  90.   return SendCommand(GetDeviceId(), msg, command, param);
  91. }
  92.  
  93. //
  94. // Open the MCI device.
  95. //
  96. uint32
  97. TMci::Open(MCI_OPEN_PARMS parms, uint32 command)
  98. {
  99.   uint32 retVal = SendCommand(0, MCI_OPEN, command, (uint32)(void far*)&parms);
  100.   if (retVal == 0) {
  101.     // success
  102.     //
  103.     DeviceId = parms.wDeviceID;
  104.   }
  105.   return retVal;
  106. }
  107.  
  108. //
  109. // Stops the MCI device and closes it.
  110. //
  111. uint32
  112. TMci::Close()
  113. {
  114.   SetBusy(false);
  115.   Stop(MCI_WAIT);
  116.   return SendCommand(MCI_CLOSE, MCI_WAIT, 0);
  117. }
  118.  
  119. //
  120. // Stop the MCI device.
  121. //
  122. uint32
  123. TMci::Stop(uint32 flags)
  124. {
  125.   SetBusy(false);
  126.   MCI_GENERIC_PARMS parms;
  127.  
  128.   if (flags & MCI_NOTIFY)
  129.     parms.dwCallback = GetCallback();
  130.  
  131.   uint32 retVal = SendCommand(MCI_STOP, flags, (uint32)(void far*)&parms);
  132.   SetBusyIfNeeded(flags);
  133.   return retVal;
  134. }
  135.  
  136. //
  137. // Pause the MCI device.
  138. //
  139. uint32
  140. TMci::Pause(uint32 flags)
  141. {
  142.   SetBusy(false);
  143.   MCI_GENERIC_PARMS parms;
  144.  
  145.   if (flags & MCI_NOTIFY)
  146.     parms.dwCallback = GetCallback();
  147.  
  148.   uint32 retVal = SendCommand(MCI_PAUSE, flags, (uint32)(void far*)&parms);
  149.   SetBusyIfNeeded(flags);
  150.   return retVal;
  151. }
  152.  
  153. //
  154. // Resume playing of the MCI device.
  155. //
  156. uint32
  157. TMci::Resume(uint32 flags)
  158. {
  159.   SetBusy(false);
  160.   MCI_GENERIC_PARMS parms;
  161.  
  162.   if (flags & MCI_NOTIFY)
  163.     parms.dwCallback = GetCallback();
  164.  
  165.   uint32 retVal = SendCommand(MCI_RESUME, flags, (uint32)(void far*)&parms);
  166.   SetBusyIfNeeded(flags);
  167.   return retVal;
  168. }
  169.  
  170. //
  171. // Set the busy flag if the command included MCI_NOTIFY.
  172. //
  173. void
  174. TMci::SetBusyIfNeeded(uint32 command)
  175. {
  176.   if (command & MCI_NOTIFY)
  177.     SetBusy(true);
  178. }
  179.  
  180. //
  181. // Play the MCI device.
  182. //
  183. uint32
  184. TMci::Play(MCI_PLAY_PARMS parms, uint32 flags)
  185. {
  186.   uint32 retVal = SendCommand(MCI_PLAY, flags, (uint32)(void far*)&parms);
  187.   SetBusyIfNeeded(flags);
  188.   return retVal;
  189. }
  190.  
  191. //
  192. // Seek to a particular position on the MCI device.
  193. // This function requires the parameters are specified in a structure.
  194. //
  195. uint32
  196. TMci::Seek(MCI_SEEK_PARMS parms, uint32 flags)
  197. {
  198.   uint32 retVal = SendCommand(MCI_SEEK, flags, (uint32)(void far*)&parms);
  199.   SetBusyIfNeeded(flags);
  200.   return retVal;
  201. }
  202.  
  203. //
  204. // Seek to a particular position on the MCI device.
  205. // Function provided for convenience.
  206. //
  207. uint32
  208. TMci::Seek(uint32 to, uint32 flags)
  209. {
  210.   MCI_SEEK_PARMS parms;
  211.  
  212.   if (flags & MCI_NOTIFY)
  213.     parms.dwCallback = GetCallback();
  214.  
  215.   parms.dwTo = to;
  216.  
  217.   return Seek(parms, flags);
  218. }
  219.  
  220. //
  221. // Load the file into the MCI device.
  222. //
  223. uint32
  224. TMci::Load(const char far* fileName, uint32 flags)
  225. {
  226.   MCI_LOAD_PARMS parms;
  227.  
  228.   flags |= MCI_LOAD_FILE;
  229.  
  230.   if (flags & MCI_NOTIFY)
  231.     parms.dwCallback = GetCallback();
  232.  
  233.   parms.lpfilename = fileName;
  234.   return SendCommand(MCI_LOAD, flags, (uint32)(void far*)&parms);
  235. }
  236.  
  237. //
  238. // Default MciNotify is to return 0.
  239. //
  240. TResult
  241. TMci::MciNotify(TParam1, TParam2)
  242. {
  243.   return 0;
  244. }
  245.  
  246. //
  247. // Send the MCI command to the opened device.
  248. //
  249. uint32
  250. TMci::SendCommand(uint deviceId, uint msg, uint32 command, uint32 param)
  251. {
  252.   return ::mciSendCommand(deviceId, msg, command, param);
  253. }
  254.